package me.flyray.crm.core.api;

import java.util.Map;

import javax.validation.Valid;

import com.github.wxiaoqi.security.biz.common.crm.request.*;
import com.github.wxiaoqi.security.common.util.EntityUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import me.flyray.crm.core.biz.CommonAccountQueryBiz;
import me.flyray.crm.core.biz.CommonAccountTransferBiz;
import me.flyray.crm.core.biz.CommonFreezeUnFreezeBiz;
import me.flyray.crm.core.biz.CommonIntoAccountBiz;
import me.flyray.crm.core.biz.CommonOutAccountBiz;
import me.flyray.crm.core.biz.PayChannelConfigBiz;
import me.flyray.crm.core.entity.PayChannelConfig;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags="账户管理")
@Controller
@RequestMapping("businessApi")
public class BusinessRestApi{
	private final static Logger logger = LoggerFactory.getLogger(BusinessRestApi.class);
	
	@Autowired
	private PayChannelConfigBiz payChannelConfigBiz;
	@Autowired
	private CommonIntoAccountBiz commonIntoAccountBiz;
	@Autowired
	private CommonOutAccountBiz outAccountBiz;
	@Autowired
	private CommonAccountTransferBiz commonAccountTransferBiz;
	@Autowired
	private CommonFreezeUnFreezeBiz commonFreezeUnFreezeBiz;
	@Autowired
	private CommonAccountQueryBiz commonAccountQueryBiz;
	
	/**
	 * 手续费计算
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/feeManage/calculate", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> queryMerChannelFee(@RequestBody @Valid MerChannelFeeRequest merChannelFeeRequest){
		Map<String, Object> param = EntityUtils.beanToMap(merChannelFeeRequest);
		Map<String, Object> channelConfig = payChannelConfigBiz.getPayChannelFee(param);
		return channelConfig;
	}
	
	/**
	 * 余额充值
	 * 入账（支付）
	 * @return
	 * @throws Exception 
	 */
	@ApiOperation("个人或者商户入账相关的接口")
	@RequestMapping(value = "/accounting/balanceRecharge", method = RequestMethod.POST)
	@ResponseBody
    public Map<String, Object> intoAccount(@RequestBody @Valid IntoAccountRequest intoAccountRequest){
		Map<String, Object> response = commonIntoAccountBiz.intoAccount(intoAccountRequest);
		return response;
    }
	
	/**
	 * 余额支付
	 * @return
	 * @throws Exception 
	 */
	@ApiOperation("个人或者商户转账相关的接口")
	@RequestMapping(value = "/accounting/balancePay", method = RequestMethod.POST)
	@ResponseBody
    public Map<String, Object> accountTransfer(@RequestBody @Valid AccountTransferRequest accountTransferRequest){
		Map<String, Object> response = commonAccountTransferBiz.accountTransfer(accountTransferRequest);
		return response;
    }
	
	/**
	 * 出账（退款）
	 * @return
	 * @throws Exception 
	 */
	@ApiOperation("个人或者商户直接出账的接口")
	@RequestMapping(value = "/accounting/outAccount", method = RequestMethod.POST)
	@ResponseBody
    public Map<String, Object> outAccount(@RequestBody @Valid OutAccountRequest outAccountRequest){
		Map<String, Object> response = outAccountBiz.outAccount(outAccountRequest);
		return response;
    }
	
	/**
	 * 冻结（退款申请）
	 * 余额冻结（提现申请）
	 * @return
	 * @throws Exception 
	 */
	@ApiOperation("冻结接口")
	@RequestMapping(value = "/accounting/freeze", method = RequestMethod.POST)
	@ResponseBody
    public Map<String, Object> freeze(@RequestBody @Valid FreezeRequest freezeRequest){
		Map<String, Object> response = commonFreezeUnFreezeBiz.freeze(freezeRequest);
		return response;
	}
	
	/**
	 * ======解冻返还账户===================
	 * 解冻（退款审核失败）
	 * 余额解冻（提现审核失败）
	 * @return
	 * @throws Exception 
	 */
	@ApiOperation("解冻接口")
	@RequestMapping(value = "/accounting/unFreeze", method = RequestMethod.POST)
	@ResponseBody
    public Map<String, Object> unfreeze(@RequestBody @Valid UnFreezeRequest unFreezeRequest){
		Map<String, Object> response = commonFreezeUnFreezeBiz.unfreeze(unFreezeRequest);
		return response;
    }

	/**
	 * ======解冻扣除金额===================
	 * 解冻并出账（退款审核）
	 * 余额解冻并支付（提现审核）
	 * @return
	 * @throws Exception 
	 */
	@ApiOperation("个人或者商户解冻并出账的接口")
	@RequestMapping(value = "/accounting/unFreAndOutAccount", method = RequestMethod.POST)
	@ResponseBody
    public Map<String, Object> unFreAndOutAccount(@RequestBody @Valid UnFreezeAndOutAccountRequest unFreAndOutAccountRequest){
		Map<String, Object> response = outAccountBiz.unFreAndOutAccount(unFreAndOutAccountRequest);
		return response;
	}
	
	/**
	 * ======解冻并出账===================
	 * 解冻并出账至余额账户（退款审核）
	 * @return
	 * @throws Exception 
	 */
	@ApiOperation("解冻并出账至余额账户")
	@RequestMapping(value = "/accounting/unFreAndOutAccToBalance", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> unFreAndOutAccToBalance(@RequestBody @Valid UnFreezeAndTransferRequest unFreezeAndTransferRequest){
		Map<String, Object> response = outAccountBiz.unfreezeAndTransfer(unFreezeAndTransferRequest);
		logger.info("解冻并出账:{}",response);
		return response;
    }
	
	
	/**
	 * 订单状态查询
	 * @return
	 * @throws Exception 
	 */
	@ApiOperation("订单状态查询")
	@RequestMapping(value = "/accounting/queryOrderInfo", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> queryOrderInfo(@RequestBody @Valid AccountingOrderInfoReq accountingOrderInfoReq){
		Map<String, Object> response = commonAccountQueryBiz.queryOrderInfo(accountingOrderInfoReq);
		logger.info("订单状态查询返回:{}",response);
		return response;
    }

}
